package com.css.apps.base.role.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.model.SUserRole;
import com.css.apps.base.user.service.UserHelper;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class DelRoleUser extends AdminAction {
	
	private static Log log = LogFactory.getLog(DelRoleUser.class);
	private String ids; // 用户uuid
	private String roleId; // 角色uuid

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(ids) || StringHelper.isEmpty(roleId)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			List<String> listIds = StringHelper.strToList(ids);
			List<String> sb = new QueryCache(
					"select a.uuid from SUserRole a where a.roleId= :roleId and a.userId in (:listIds) ")
							.setParameter("roleId", roleId).setParameter("listIds", listIds).list();
			List<SUserRole> userroleList = QueryCache.idToObj(SUserRole.class, sb);
			UserHelper helper = new UserHelper();
			for (SUserRole userroleObj : userroleList) {
				SUser usrObj = QueryCache.get(SUser.class, userroleObj.getUserId());
				usrObj.getRoleList().removeAll();
				helper.clearUserMenuCache(userroleObj.getUserId());
			}
			tx = new TransactionCache();
			tx.delete(SUserRole.class, sb);
			tx.commit();

			SRole sRole = QueryCache.get(SRole.class, roleId);
			delDeskRole(sRole, listIds);

			for (String userRoleid : sb) {
				LogPart lp = new LogPart();
				lp.setOpObjType(SUserRole.class.getName());
				lp.setOpObjId(userRoleid);
				lp.setRelObjType(SRole.class.getName());
				lp.setRelObjId(roleId);
				lp.setOpType(LogConstant.LOG_TYPE_DELETE);
				lp.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
				lp.setLogData("");
				lp.setResult(LogConstant.RESULT_SUCCESS);
				lp.save();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	@SuppressWarnings("unchecked")
	public void delDeskRole(SRole sRole, List<String> userIdList) {
		List<String> addSys = new ArrayList<>();
		// 循环取用户所具有的sysId列表
		for (String userId : userIdList) {
			QueryCache qc = new QueryCache("select DISTINCT a.sysId from SUserRole a  where a.userId=:userId ")
					.setParameter("userId", userId);
			List<String> sysIds = qc.list();
			if (sysIds == null || sysIds.size() == 0) {
				// 原本没有权限则加
				addSys.add(sRole.getSysId());
			} else {
				// 如有则对比
				boolean flag = true;
				for (String oldsysId : sysIds) {
					if (sRole.getSysId().equals(oldsysId)) {
						// 对比成功，则不添加该系统
						flag = false;
					}
				}
				if (flag) {
					addSys.add(sRole.getSysId());
				}
			}
		}
	}
}
